<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="css/style.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" href="artDialog/skins/default.css" type="text/css"></link>
<link href="css/select.css" rel="stylesheet" type="text/css" />
<title>DEMO</title>
<script type="text/javascript" src="js/apiv1.3.min.js"></script>
<!--script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script-->
<link rel="stylesheet" type="text/css" href="bmap.css" />
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript" src="js/select-ui.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/jquery.artDialog.min.js"></script>
<script type="text/javascript" src="artDialog/artDialog.plugins.min.js"></script>
<script type="text/javascript" src="js/util.js"></script>

</head>
<body>
	<div class="place">
		<span>位置：</span>
		<ul class="placeul">
			<li><a href="#">首页</a>
			</li>
			<li><a href="#">巡检任务管理</a>
			</li>
			<li><a href="#">巡检任务回执</a>
			</li>
		</ul>
		<span style="color: FF6633;">左键塔杆录入右键回执</span>
	</div>

	<div
		style="width:100%;height:100%;border:1px solid gray;position:absolute;"
		id="container"></div>
		

</body>
</html>
<script type="text/javascript">
	
		
	var mapOptions = {
		mapType : BMAP_NORMAL_MAP,
		maxZoom : 10, //地图最大层级
		minZoom : 5
	};
	
	// 百度地图API功能
	var map = new BMap.Map("container", mapOptions); 		// 创建Map实例
	var initPoint = new BMap.Point(116.404, 39.915); // 创建点坐标
	map.centerAndZoom(initPoint, 8);  // 初始化地图,设置中心点坐标和地图级别。
	//添加地图类型控件
	map.addControl(new BMap.MapTypeControl({
		mapTypes : [ BMAP_NORMAL_MAP, BMAP_HYBRID_MAP ]
	}));
	map.setCurrentCity("北京");			 // 设置地图显示的城市 此项是必须设置的
	map.enableScrollWheelZoom(true);    //开启鼠标滚轮缩放
	map.enableKeyboard(); 				// 启用键盘操作。  
	map.enableContinuousZoom();        //启用连续缩放效果



	// ----- control -----
	map.addControl(new BMap.NavigationControl()); //地图平移缩放控件
	map.addControl(new BMap.ScaleControl()); //显示比例尺在右下角
	
	var overView = new BMap.OverviewMapControl();
	var overViewOpen = new BMap.OverviewMapControl({isOpen:true, anchor: BMAP_ANCHOR_BOTTOM_RIGHT});
	map.addControl(overView);          //添加默认缩略地图控件
	map.addControl(overViewOpen);      //右下角，打开

	// ----- Hide or Show Pole -----
	map.addEventListener("zoomend", function() {
		var overlays = map.getOverlays();
	    for ( var i = 0; i < overlays.length - 1; i++) {
			if (overlays[i]._config.icon) {
				 if (overlays[i]._config.icon.imageUrl.indexOf("images/circle")!=-1) {
					if (map.getViewport().zoom <= 7) {
						overlays[i].hide();
					} else {
						overlays[i].show();
					}
				} 
			} 
		}
	});
	
	
	/* ----自定义控件---- */
	// 定义一个控件类,即function
	function ZoomControl(){
	  // 默认停靠位置和偏移量
	  this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
	  this.defaultOffset = new BMap.Size(75, 20);
	}
	
	function ZoomControl2(){
	  // 默认停靠位置和偏移量
	  this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
	  this.defaultOffset = new BMap.Size(135, 20);
	}
	
	
	// 通过JavaScript的prototype属性继承于BMap.Control
	ZoomControl.prototype = new BMap.Control();
	ZoomControl2.prototype=new BMap.Control();

	// 自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
	// 在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
	ZoomControl.prototype.initialize = function(map){
	  // 创建一个DOM元素
	  var div = document.createElement("div");
	  // 添加文字说明
	  div.appendChild(document.createTextNode("放大2级"));
	  // 设置样式
	  div.style.cursor = "pointer";
	  div.style.border = "2px solid gray";
	  div.style.backgroundColor = "white";
	  // 绑定事件,点击一次放大两级
	  div.onclick = function(e){
		map.setZoom(map.getZoom() + 2);
	  };
	  // 添加DOM元素到地图中
	  map.getContainer().appendChild(div);
	  // 将DOM元素返回
	  return div;
	};
	
	ZoomControl2.prototype.initialize = function(map){
	  // 创建一个DOM元素
	  var div = document.createElement("div");
	  // 添加文字说明
	  div.appendChild(document.createTextNode("缩小2级"));
	  // 设置样式
	  div.style.cursor = "pointer";
	  div.style.border = "2px solid gray";
	  div.style.backgroundColor = "white";
	  // 绑定事件,点击一次放大两级
	  div.onclick = function(e){
		map.setZoom(map.getZoom() - 2);
	  };
	  // 添加DOM元素到地图中
	  map.getContainer().appendChild(div);
	  // 将DOM元素返回
	  return div;
	};
	
	
	// 创建控件
	var myZoomCtrl = new ZoomControl();
	var myZoomCtrl2 = new ZoomControl2();
	// 添加到地图当中
	map.addControl(myZoomCtrl);
	map.addControl(myZoomCtrl2);
	



	function addPole(lng, lat, opts,color) {
		var point = new BMap.Point(lng, lat);
		var myIcon = new BMap.Icon("images/circle"+color+".png", new BMap.Size(16, 16));

		var marker = new BMap.Marker(point, {
			icon : myIcon,
			enableDragging : false,
			draggingCursor : "move",
			title : opts
		});
		var contextMenu = new BMap.ContextMenu();
		var assignMenu = new BMap.MenuItem("上传回执记录",function(e,p,type){
				var poleNo=type._config.title;
				$.post("patrolTask!findTaskNoByPidAndUid.action",{"state":"执行中","pid":poleNo},function(data){
					var task =eval("("+data+")");
					if(task.length==1){
						over_task(task[0].id,task[0].name,poleNo);
					}else{
							var taskSelect = "<select class=\"taskSelect\" id='taskSelect'>";
							for(var i = 0;i<task.length;i++){
								taskSelect+="<option value='"+task[i].id+"'>"+task[i].name+"</option>"
							}
								taskSelect+="</select>";
							art.dialog({title:"选择任务",content:taskSelect,lock:true,fixed:true,okValue:"选择",cancelValue:"取消",ok:function(){
									$("#taskSelect").children().each(function(){
									if($(this).attr("selected")){
										over_task($(this).val(),$(this).text(),poleNo);
									}
								});
									return true;
								},cancel:function(){}});
					$(".taskSelect").uedSelect({width:180});
					}
				});					
				},{width:80});
		contextMenu.addItem(assignMenu);
		marker.addContextMenu(contextMenu);
	
		 
		marker.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y)
					.css("left", x).css("z-index", 1);
			routeDiv.html("<span style=\"font-size:12px;font-weight:bolder\">"
					+ opts + "</span>");
		});
		marker.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
		});
		marker.addEventListener("click", function(type, target, point,
				pixel) {
				$.post("patrolTask!findTaskNoByPidAndUid.action",{"state":"执行中","pid":opts},function(data){
					var task =eval("("+data+")");
					if(task.length>0){
						receipt_task(task[0].id,task[0].name,opts);
					}else{
					/*	$.post("receiptPatrol!findByPidAndPtid.action",{pid:opts,ptid:task[0].id},function(r){
							var t = eval("("+r+")");
							execute_task(t.id,t.name,opts);
						});*/
					}
			
				});
		});
		
		map.addOverlay(marker);
		return marker;
	}
	function over_task(taskNo,taskName,poleNo){
		okCancelAlert("上传回执记录","是否上传《"+taskName+"》的回执记录",function(){
			$.post("receiptPatrol!over.action",{ptid:taskNo},function(data){
				var json =eval("("+data+")");
						if(json.code==1){
							defaultAlert("上传回执记录","上传成功");
							/* location.href="wait.html".reload(true); */
						}else if(json.code==3){
							defaultAlert("上传回执记录","请先录入");
						}else{
							defaultAlert("上传回执记录","上传失败");
						}
						setTimeout(function () { location.reload(true)}, 2000);
					
			});
		},function(){});
	}
	function receipt_task(taskNo,taskName,poleNo){
	$.post("receiptPatrol!findByPidAndPtid.action",{pid:poleNo,ptid:taskNo},function(r){
							var task = eval("("+r+")");
							var df_bid=-1;
							var df_lid=-1;
							var df_desc="";
							var df_rate="";
							if(task!=null){
								df_bid=task.bid;
								df_lid=task.level;
								df_desc=task.description;
								df_rate=task.intact;
							}
			$.post("bugType!findList.action",function(data){
				var bts = eval("("+data+")");
				var btSelect = "<div class='vocation'><select class='btSelect'>"
				for(var i = 0;i<bts.length;i++){
					btSelect+="<option value='"+bts[i].id+"' "+(bts[i].id==df_bid?"selected='selected'":"")+">"+bts[i].name+"</option>";
				}
				btSelect+="</select></div>";
				var levelSelect ="<div class='vocation'><select class='levelSelect'>";
				levelSelect+="<option value='1' "+(df_lid==1?"selected='selected'":"")+">一般</option>";
				levelSelect+="<option value='2' "+(df_lid==2?"selected='selected'":"")+">严重</option>";
				levelSelect+="<option value='3' "+(df_lid==3?"selected='selected'":"")+">紧急</option>";
				levelSelect+="</select><div>";
				var html="<ul class='forminfo'><li><label>任务名称<b>*</b></label><input  type='text' class='dfinput' value='"+taskName+"' disabled='disabled' /></li>";
				html+="<li><label>杆塔编号<b>*</b></label><input  type='text' class='dfinput' value='"+poleNo+"' disabled='disabled'/></li>";
				html+="<li><label>缺陷类型<b>*</b></label>"+btSelect+"<label>&nbsp;&nbsp;&nbsp;&nbsp;缺陷等级<b>*</b></label>"+levelSelect+"</li>";
				html+="<li><label>完好率<b>*</b></label><input  type='text' id='rate' class='dfinput' value='"+df_rate+"' /></li>";
				html+="<li><label>缺陷描述<b>*</b></label><textarea id='description'  class='dfinput' style='width:345px;height:120px;'>"+df_desc+"</textarea></li>";
				html+="</ul>";
				art.dialog({title:"巡检任务回执录入",content:html,okValue:"录入",cancelValue:"取消",cancel:function(){},ok:function(){
					var rate = document.getElementById("rate");
					var desc = document.getElementById("description");
					if(rate.value==""){
						rate.select();
						rate.focus();
						this.shake();
						alert("请填入完好率");
						return false;
					}
					if(isNaN(rate)){
						rate.select();
						rate.focus();
						this.shake();
						alert("请输入数字");
						return false;
					}
					if(desc.value==""){
						desc.select();
						desc.focus();
						this.shake();
						alert("请输入缺陷描述");
						return false;
					}
					$.post("receiptPatrol!receipt.action",{
						"receiptpatrol.bid":$(".btSelect").val(),
						"receiptpatrol.intact":rate.value,
						"receiptpatrol.level":$(".levelSelect").val(),
						"receiptpatrol.pid":poleNo,
						"receiptpatrol.ptid":taskNo,
						"receiptpatrol.description":desc.value
					},function(data){
						var json =eval("("+data+")");
						if(json.code==1){
							defaultAlert("巡检任务回执录入","录入成功");
						}else{
							defaultAlert("巡检任务回执录入","录入失败");
						}
						setTimeout(function () { 
							location.reload(true);
						}, 2000);
					});
			//
				}});
			$(".btSelect").uedSelect({width:130});
			$(".levelSelect").uedSelect({width:130});
			
			});
		
		});
				
	}
	
	
	var routeDiv;
	function addRoute(points, opts, color) {

		var polyline = new BMap.Polyline(points, {
			strokeColor : color,
			strokeWeight : 3,
			strokeOpacity : 0.8,
			enableClicking :true
		});
		
		polyline.addEventListener("mouseover", function(type, target, point,
				pixel) {
			var x = type.pixel.x;
			var y = type.pixel.y;
			$("body").append("<div id =\"routeDiv\"></div>")
			routeDiv = $("#routeDiv").css("position", "absolute").css("top", y)
					.css("left", x).css("z-index", 1);
			routeDiv.html("<span style=\"font-size:12px;font-weight:bolder\">"
					+ opts + "</span>");
					
		});
		polyline.addEventListener("mouseout", function() {
			if (routeDiv) {
				routeDiv.remove();
			}
			
		});
		
		
		map.addOverlay(polyline);
	}

	function calc_length(html) {
		var arr = html.split("<br>");
		var spoint = getPointByPointTitle($("#startNO").html());//获取起点point
		var epoint = getPointByPointTitle($("#endNO").html());//获取终点point
		var length = 0;
		if(arr.length>1){
			length=map.getDistance(spoint,getPointByPointTitle(arr[0]));
			
			for(var m = 1;m<arr.length;m++){
				length+=map.getDistance(getPointByPointTitle(arr[m-1]),getPointByPointTitle(arr[m]));
			}
			
			length +=map.getDistance(getPointByPointTitle(arr[arr.length-1]),epoint);
			
		}else{
			length=map.getDistance(spoint,epoint);
		}
		length =(length/1000)+"";
		var tmp = length.split(".");
		length = tmp[0]+"."+tmp[1].substring(0,3)+"公里";
		
		return length;	
	}
	function getPointByPointTitle(title){
		var overlays = map.getOverlays();
			for ( var i = 0; i < overlays.length ; i++) {
			if (overlays[i]._config) {
				if (overlays[i]._config.title==title) {
					return overlays[i]._config.point;
				}
			}
		}
	}
	
//findAllPolies
	$.post("patrolTask!findExecuting.action",function(data){
			var json = eval("("+data+")");
			for(var i=0;i<json.length;i++){
				var polies = json[i];
				var points = new Array();
				var name = "";
				var points1 = new Array();
				var colors =["","","_yellow","_red"];
				for ( var x = 0; x < json[i].polies.length; x++) {
					points[x] = new BMap.Point(json[i].polies[x].lng,json[i].polies[x].lat);
					var color= colors[1];
					if(json[i].polies[x].receiptpatrol!=null){
						color = colors[json[i].polies[x].receiptpatrol.level];
					}
					addPole(json[i].polies[x].lng, json[i].polies[x].lat, json[i].polies[x].id,color);
				}
				addRoute(points, json[i].name, "green");
  				
			}
	});
</script>
